<?php


namespace app\common\model;


use addons\wdsxh\library\Wxapp;
use app\admin\model\wdsxh\UserBelong;
use think\Model;

class BusinessModel extends Model
{
    protected $name = 'wdsxh_business';


    static public function sendNewBusinessNotice($business)
    {
        ini_set('max_execution_time', '0');
        try {
            $conf = (new \app\admin\model\wdsxh\Config())->where('id', 1)->find();
            $time = explode('-', $business['time']);
            $startTime = strtotime(trim($time[0]));
            mb_internal_encoding("UTF-8");
            $thing3 = '';
            $member = model('app\admin\model\wdsxh\Member')->where('uid', $business['uid'])->find();
            if($member) {
                $thing3 = $member['name'];
                $member_unit = model('app\admin\model\wdsxh\MemberUnit')->where('member_id', $member['id'])->find();
                if($member_unit) {
                    $thing3 = $member_unit['company_name'];
                }
            }

            $data = [
                'thing1' => [
                    'value' => mb_substr($business['title'], 0, 20),
                ],
                'thing3' => [
                    'value' => mb_substr($thing3, 0, 20),
                ],
                'thing4' => [
                    'value' => $business['brief']?mb_substr($business['brief'], 0, 20):'',
                ],
                'time5' => [
                    'value' => date('Y年m月d日 H:i', $startTime),
                ]
            ];

            $member_ids = model('app\admin\model\wdsxh\MemberUnit')->whereIn('id',$business['member_unit_ids'])->column('member_id');
            $userIdArray = model('app\admin\model\wdsxh\Member')->whereIn('id', $member_ids)->column('uid');
            $userWhere['user_id'] = array('in', $userIdArray);
            $userLists = (new UserBelong())->where($userWhere)->field('openid')->select();
            $users = [];
            foreach ($userLists as $row) {
                $users[] = $row['openid'];
            }
            $res = Wxapp::subscribeMessage($conf['business_new'], $users, '/pages/Functions/index', $data);
        } catch (\think\Exception $e) {
        }
    }


}